////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2008, 2009 The Mist project                                  //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains the types and functions used to classify compiler messages.
///

#ifndef MESSAGEINFO_HPP
#define MESSAGEINFO_HPP

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <map>
using std::map;

#include <string>
using std::string;

#include "common/singleton.hpp"

////////////////////////////////////////////////////////////////////////////////
// Extra Types                                                                //
////////////////////////////////////////////////////////////////////////////////

/// The severity of a message.
///
enum MessageSeverity {
	Error,  ///< an error which will prevent full compilation
	Warning ///< a warning which should be fixed but does not prevent compilation
};

/// Enum representing the way location information collects inside one message
///
enum MessageType {
	NoLocation,       ///< a message without a location
	SingleLocation,   ///< a message has one location, specified once
	MultipleLocation, ///< a message may have multiple unconnected locations, collected by (code, %1%)
	CompoundLocation  ///< a message has one location, collected by (code, location-left-to-right); also builds up %1%
};

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

/// A singleton class which provides access to message templates from codes.
///
class MessageInfo {
	public:
		
		friend class Singleton<MessageInfo>;
		
		/// Add an extra message type on the fly. For example, a syntax error generated by the parser.
		///
		/// \return the complete code of this particular message, of the form base:nr,
		///         in which base was supplied by the argument \a code and nr is a unique
		///         number; the numbers are not guaranteed to be consecutive
		///
		string extra(
			const string& code,       ///< the base code of the new message; choose an appropriate code
			MessageSeverity severity, ///< the severity of the new message
			const string& message     ///< the template associated with the new message
		);
		
		/// \return true , if a given \a code exists
		///         false, otherwise
		///
		bool code_exists(const string& code) const;
		
		/// \return the severity of the message code \a code
		///
		MessageSeverity severity(const string& code) const;
		
		/// \return the message template of the message code \a code
		///
		const string& message(const string& code) const;
		
		/// \return the message location type belonging to the message code \a code
		///
		MessageType type(const string& code) const;
	
	protected: // functions
		
		MessageInfo();
	
	private: // types
		
		struct Info {
			Info() {}
			Info(MessageSeverity s, const string& m, MessageType t) : severity(s), message(m), type(t) {}
			MessageSeverity severity;
			string message;
			MessageType type;
		};
		
		typedef map<string, Info>::const_iterator It;
	
	private: // variables
		
		map<string, Info> _info;
		uint _extraIndex;
};

////////////////////////////////////////////////////////////////////////////////
// Function Declarations                                                      //
////////////////////////////////////////////////////////////////////////////////

/// \return the textual representation of the message severity \a severity
///
string to_string(MessageSeverity severity);

#endif // MESSAGEINFO_HPP
